package org.lalib.controller.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.model.SelectItem;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CWaiterForm;
import org.lalib.controller.form.CWaiterForm.SummaryBean;
import org.lalib.controller.service.CFoodService;
import org.lalib.controller.service.CHistoricalValueService;
import org.lalib.controller.service.COrderService;
import org.lalib.controller.service.CTableService;
import org.lalib.controller.service.CUserService;
import org.lalib.entity.object.EFood;
import org.lalib.entity.object.EHistoricalValue;
import org.lalib.entity.object.EItem;
import org.lalib.entity.object.EOrder;
import org.lalib.entity.object.ETable;
import org.lalib.entity.object.EUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller("cWaiterAction")
public class CWaiterAction implements IControllerAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	CWaiterForm cWaiterForm;

	@Autowired
	CUserService cUserService;

	@Autowired
	CFoodService cFoodService;

	@Autowired
	CTableService cTableService;

	@Autowired
	COrderService cOrderService;

	@Autowired
	CHistoricalValueService cHistoricalValueService;

	public String init(EItem item) {
		EUser user = (EUser) item;
		user = cUserService.getUserById(user.getId());
		List<ETable> tables = user.getTables();
		cWaiterForm.setUser(user);
		cWaiterForm.setTables(tables);
		List<EFood> allFoods = cFoodService.getAllFoods();
		cWaiterForm.setFoods(allFoods);
		cWaiterForm.setFoodSelectItems(getSelectItemsForFoods(allFoods));
		return "mobileWaiter";
	}

	public String navigateTable(ETable table) {
		cWaiterForm.setSelectedTable(table);
		resetValues(table);
		return "mobileWaiterTable";
	}

	private List<SelectItem> getSelectItemsForFoods(List<EFood> foods) {
		List<SelectItem> selectItems = new ArrayList<>();
		for (EFood food : foods)
			selectItems.add(new SelectItem(food.getId(), food.getName()));
		return selectItems;
	}

	public void addFood() {
		Long selectedFood = cWaiterForm.getSelectedFood();

		EFood food = cFoodService.getFoodById(selectedFood);
		ETable table = cTableService.getTableById(cWaiterForm.getSelectedTable().getId());
		EUser user = cUserService.getUserById(cWaiterForm.getUser().getId());
		String description = cWaiterForm.getDescription();

		EOrder order = new EOrder();
		order.setDate(new Date());
		order.seteFood(food);
		order.seteTable(table);
		order.seteUser(user);
		order.setExplanation(description);
		cOrderService.save(order);
		resetValues(table);
	}

	public void updateFood() {
		Long selectedFood = cWaiterForm.getSelectedFood();

		EFood food = cFoodService.getFoodById(selectedFood);
		ETable table = cTableService.getTableById(cWaiterForm.getSelectedTable().getId());
		String description = cWaiterForm.getDescription();

		EOrder order = cOrderService.getOrderById(cWaiterForm.getCurrentOrder().getId());
		order.seteFood(food);
		order.seteTable(table);
		order.setExplanation(description);
		cOrderService.update(order);
	}

	private void resetValues(ETable table) {
		cWaiterForm.setDescription("");
		cWaiterForm.setOrdersForTable(cOrderService.getCurrentOrders(table));
	}

	public String goBack() {
		return init(cWaiterForm.getUser());
	}

	public String logout() {
		return "mobileLogin";
	}

	public String navigateToFoodDetails(EOrder order) {
		cWaiterForm.setCurrentOrder(order);
		cWaiterForm.setSelectedFood(order.geteFood().getId());
		cWaiterForm.setDescription(order.getExplanation());
		return "mobileWaiterFood";
	}

	public String delete() {
		EOrder order = cWaiterForm.getCurrentOrder();
		order = cOrderService.getOrderById(order.getId());
		cOrderService.delete(order);
		return navigateTable(cWaiterForm.getSelectedTable());
	}

	@SuppressWarnings("rawtypes")
	public String navigateToSummary() {
		List<EOrder> orders = cWaiterForm.getOrdersForTable();
		List<SummaryBean> summaries = new ArrayList<>();
		Double total = (double) 0;
		for (EOrder order : orders) {
			order = (EOrder) cOrderService.getOrderById(order.getId());
			EFood food = cFoodService.getFoodById(order.geteFood().getId());
			EHistoricalValue value = cHistoricalValueService.getValue(food, "fiyat");
			Double tutar = Double.valueOf("0");
			if (value != null && value.getCore() != null) {
				BigDecimal val = (BigDecimal) value.getCore();
				tutar = val.doubleValue();
			}
			total += tutar;
			summaries.add(cWaiterForm.new SummaryBean(food.getName(), tutar, order.getId()));
		}
		cWaiterForm.setSummaryBeans(summaries);
		cWaiterForm.setTotal(total);

		return "mobileWaiterSummary";
	}

	public void reTotal() {
		List<SummaryBean> summaryBeans = cWaiterForm.getSummaryBeans();
		Double total = (double) 0;
		for (SummaryBean bean : summaryBeans)
			if (bean.isSelected())
				total += bean.getTotal();
		cWaiterForm.setTotal(total);
	}

	public void getMoney() {
		List<SummaryBean> summaryBeans = cWaiterForm.getSummaryBeans();
		List<SummaryBean> lackOfBeans = new ArrayList<>();
		for (SummaryBean bean : summaryBeans) {
			if (bean.isSelected()) {
				EOrder order = cOrderService.getOrderById(bean.getOrderId());
				order.setOdendi(true);
				cOrderService.update(order);
			} else
				lackOfBeans.add(bean);
		}
		cWaiterForm.setSummaryBeans(lackOfBeans);
	}
}